package com.dongdongshop.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.domain.model.TbTypeTemplate;
import com.dongdongshop.domain.model.TbTypeTemplateExample;
import com.dongdongshop.domain.vo.ItemCatVO;
import com.dongdongshop.domain.vo.SpecificationOptionVO;
import com.dongdongshop.domain.vo.SpecificationVO;
import com.dongdongshop.domain.vo.TypeTemplateVO;
import com.dongdongshop.mapper.TbTypeTemplateMapper;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.service.TypeTemplateService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TypeTemplateServiceImpl implements TypeTemplateService {

    @Autowired
    private TbTypeTemplateMapper templateMapper;

    @Autowired
    private SpecificationService specificationService;

    @Override
    public PageUtil<TypeTemplateVO> selectAll(PageResult<TypeTemplateVO> parms) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        TbTypeTemplateExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(parms.getParm().getName())){
            criteria.andNameLike("%"+parms.getParm().getName()+"%");
        }
        PageHelper.startPage(parms.getPageNum(), parms.getPageSize());
        List<TbTypeTemplate> tbTypeTemplateList = templateMapper.selectByExample(example);
        PageInfo info = new PageInfo<>(tbTypeTemplateList);
        List<TypeTemplateVO> voList = tbTypeTemplateList.stream().map(tbTypeTemplate -> {
            TypeTemplateVO vo = new TypeTemplateVO();
            BeanUtils.copyProperties(tbTypeTemplate, vo);
            return vo;
        }).collect(Collectors.toList());
        return new PageUtil<>(voList, info.getTotal(), info.getPageNum(), info.getPageSize());
    }

    @Override
    public void addTemplate(TypeTemplateVO templateVO) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(templateVO, tbTypeTemplate);
        templateMapper.insertSelective(tbTypeTemplate);
    }

    @Override
    public TypeTemplateVO selectById(Long id) {
        TbTypeTemplate tbTypeTemplate = templateMapper.selectByPrimaryKey(id);
        TypeTemplateVO vo = new TypeTemplateVO();
        BeanUtils.copyProperties(tbTypeTemplate,vo);
        return vo;
    }

    @Override
    public void updateTemplate(TypeTemplateVO templateVO) {
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate();
        BeanUtils.copyProperties(templateVO, tbTypeTemplate);
        templateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
    }

    @Override
    public void delete(List<Long> ids) {
        TbTypeTemplateExample example = new TbTypeTemplateExample();
        example.createCriteria().andIdIn(ids);
        templateMapper.deleteByExample(example);
    }

    @Override
    public TypeTemplateVO getByItemCatId(Long id) {
        //根据模板id查找出对应的数据
        TbTypeTemplate tbTypeTemplate = templateMapper.selectByPrimaryKey(id);
        TypeTemplateVO templateVO = new TypeTemplateVO();
        //获取规格
        List<SpecificationVO> voList = JSONObject.parseArray(tbTypeTemplate.getSpecIds(), SpecificationVO.class);
        //获取规格选项
        List<SpecificationOptionVO> specList = specificationService.specLOptionist();
        //规格和规格选项匹配
        Map<Long, List<SpecificationOptionVO>> groupMap = specList.stream().collect(Collectors.groupingBy(o -> o.getSpecId()));
        for (SpecificationVO vo : voList) {
            List<SpecificationOptionVO> optionVOS = groupMap.get(vo.getId());
            if (optionVOS != null && !optionVOS.isEmpty()){
                vo.setSpecOptionVoList(optionVOS);
            }
        }
        templateVO.setSpecVOList(voList);
        BeanUtils.copyProperties(tbTypeTemplate, templateVO);
        return templateVO;
    }
}
